import 'package:vista/shared/models/base_frame.dart';

/// 消息类型
enum MessageType {
  text, // 文本消息
  callFailed, // 拨打失败消息
}

/// 消息拍摄
/// 存储单聊消息信息，如摄影中的拍摄般记录对话瞬间
class MessageShot extends BaseFrame {
  final String id; // 消息ID
  final String senderId; // 发送者ID
  final String receiverId; // 接收者ID
  final MessageType type; // 消息类型
  final String content; // 消息内容（文本消息时使用）
  final DateTime timestamp; // 时间戳
  final bool isRead; // 是否已读

  const MessageShot({
    required this.id,
    required this.senderId,
    required this.receiverId,
    required this.type,
    required this.content,
    required this.timestamp,
    this.isRead = false,
  });

  /// 从JSON创建消息拍摄
  factory MessageShot.fromJson(Map<String, dynamic> json) {
    return MessageShot(
      id: json['id'] as String,
      senderId: json['senderId'] as String,
      receiverId: json['receiverId'] as String,
      type: MessageType.values.firstWhere(
        (e) => e.name == json['type'] as String,
        orElse: () => MessageType.text,
      ),
      content: json['content'] as String,
      timestamp: DateTime.parse(json['timestamp'] as String),
      isRead: json['isRead'] as bool? ?? false,
    );
  }

  /// 创建已读消息副本
  MessageShot copyWithRead(bool read) {
    return MessageShot(
      id: id,
      senderId: senderId,
      receiverId: receiverId,
      type: type,
      content: content,
      timestamp: timestamp,
      isRead: read,
    );
  }

  @override
  List<Object?> get props => [
    id,
    senderId,
    receiverId,
    type,
    content,
    timestamp,
    isRead,
  ];

  @override
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'senderId': senderId,
      'receiverId': receiverId,
      'type': type.name,
      'content': content,
      'timestamp': timestamp.toIso8601String(),
      'isRead': isRead,
    };
  }
}
